package com.miniot.base.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.miniot.base.entity.Devotion;
import com.miniot.base.service.DevotionService;
import com.miniot.base.vo.DevotionListVO;
import com.miniot.base.vo.DevotionQueryVO;
import com.miniot.base.vo.SaveDevotionVO;
import com.miniot.business.base.BaseController;
import com.miniot.business.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 投入品表
 *
 * @author zzq
 * @date 2023-10-17 11:15:55
 */
@RestController
@AllArgsConstructor
@RequestMapping("/devotion" )
@Api(value = "投入品表(后台)", tags = "投入品表-后台接口")

public class DevotionController extends BaseController {

    private final DevotionService devotionService;

    /**
    * 分页查询
    * @param currentPage 当前页数
    * @param pageSize 每页数据量
    * @param devotion 查询条件
    * @return 查询结果
    */
    @GetMapping("/listPage")
    @ApiOperation(value = "库存详情", notes = "传入分页信息与查询条件")
    public R<Page<Devotion>> listDevotionPage(
            @ApiParam(value = "当前页数", required = true, defaultValue = "1") @RequestParam Long currentPage,
            @ApiParam(value = "每页数据量", required = true, defaultValue = "10") @RequestParam Long pageSize,
            @ApiParam(value = "查询条件") DevotionQueryVO devotion) {
        Page<Devotion> page = new Page<Devotion>(currentPage, pageSize);
        return R.success(devotionService.page(page,new LambdaQueryWrapper<Devotion>()
                .eq(ObjectUtils.isNotEmpty(devotion.getDevotionParentTypeId()),Devotion::getDevotionParentTypeId,devotion.getDevotionParentTypeId())
                .eq(ObjectUtils.isNotEmpty(devotion.getDevotionChildrenTypeId()),Devotion::getDevotionChildrenTypeId,devotion.getDevotionChildrenTypeId())
                .orderByDesc(Devotion::getCreateTime)),"分页信息查询成功");
    }

    /**
     * 导出
     * @param devotion 查询条件
     * @return 查询结果
     */
    @GetMapping("/export")
    @ApiOperation(value = "导出", notes = "传入查询条件")
    public R<ResponseEntity<byte[]>> export(@ApiParam(value = "查询条件") DevotionQueryVO devotion, HttpServletResponse response) throws IOException {
        return R.success(devotionService.export(devotion),"导出成功");
    }



    /**
    * 通过id查询投入品表
    * @param id id
    * @return Result
    */
    @GetMapping("/getById")
    @ApiOperation(value = "通过id查询投入品表", notes = "传入id")
    public R<Devotion> getDevotionById(@ApiParam(value = "id", required = true) @RequestParam("id") Long id) {
        return R.success(devotionService.getById(id));
    }

    /**
    * 新增投入品表
    * @param devotion 投入品表
    * @return Result
    */
    @PostMapping("/save")
    @ApiOperation(value = "新增投入品表", notes = "传入Devotion数据")
    public R<Boolean> save(@RequestBody SaveDevotionVO devotion) {
        Devotion devotion1 = new Devotion();
        BeanUtil.copyProperties(devotion,devotion1);
        Assert.isTrue(devotionService.save(devotion1), "操作失败");
        return R.success(true,"新增成功");
    }

    /**
    * 修改投入品表
    * @param devotion 投入品表
    * @return Result
    */
    @PostMapping("/updateById")
    @ApiOperation(value = "修改投入品表", notes = "传入Devotion数据")
    public R<Boolean> updateById(@RequestBody SaveDevotionVO devotion) {
        Devotion devotion1 = new Devotion();
        BeanUtil.copyProperties(devotion,devotion1);
        Assert.isTrue(devotionService.updateById(devotion1), "修改失败");
        return R.success(true,"修改成功");
    }

    /**
    * 通过id删除投入品表
    * @param id id
    * @return Result
    */
    @GetMapping("/deleteById")
    @ApiOperation(value = "通过id删除投入品表", notes = "传入id")
    public R<Boolean> deleteById(@ApiParam(value = "id", required = true) @RequestParam("id") Long id) {
        Assert.isTrue(devotionService.removeDevotionById(id), "操作失败");
        return R.success(true,"删除成功");
    }

    /**
     * @description:投入品列表
     * @author LQ
     * @date 2023/10/31 11:21
     */
    @GetMapping("/listOfInputs")
    @ApiOperation(value = "库存详情", notes = "传入分页信息与查询条件")
    public R<Page<DevotionListVO>> getListOfInputs(
            @ApiParam(value = "当前页数", required = true, defaultValue = "1") @RequestParam Long currentPage,
            @ApiParam(value = "每页数据量", required = true, defaultValue = "10") @RequestParam Long pageSize,
            @ApiParam(value = "父类型id") @RequestParam Long parentTypeId,
            @ApiParam(value = "子类型id") @RequestParam Long childrenTypeId,@ApiParam(value = "投入品名称") @RequestParam String name) {
        Page<Devotion> page = new Page<Devotion>(currentPage, pageSize);
        LambdaQueryWrapper<Devotion> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtils.isNotEmpty(parentTypeId), Devotion::getDevotionParentTypeId, parentTypeId);
        wrapper.eq(ObjectUtils.isNotEmpty(childrenTypeId), Devotion::getDevotionChildrenTypeId, childrenTypeId);
        wrapper.like(ObjectUtils.isNotEmpty(name), Devotion::getDevotionName, name);
        wrapper.orderByDesc(Devotion::getCreateTime);
        Page<Devotion> page1 = devotionService.page(page, wrapper);
        List<DevotionListVO> records = page1.getRecords().stream().map(entity -> {
            DevotionListVO vo = new DevotionListVO();
            BeanUtil.copyProperties(entity, vo);
            return vo;
        }).collect(Collectors.toList());
        Page<DevotionListVO> pageData = new Page<>();
        pageData.setCurrent(currentPage);
        pageData.setPages(pageSize);
        pageData.setTotal(page1.getTotal());
        pageData.setRecords(records);
        return R.success(pageData, "分页信息查询成功");
    }
}